package com.example.myapplication.provider;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.Category;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class CategoryItemProvider extends BaseItemProvider {

    private List<Category> data;
    private AbilitySlice abilitySlice;

    public CategoryItemProvider(List<Category> data,AbilitySlice abilitySlice){
        this.data = data;
        this.abilitySlice=abilitySlice;
    }


    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {
        if(data != null && i>=0 && i<data.size()){
            return  data.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Category category = data.get(i);
        DirectionalLayout itemLayout = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_category_first_item_template,null,false);
        Image image = (Image) itemLayout.findComponentById(ResourceTable.Id_first_category_icon_image);
        Text name = (Text) itemLayout.findComponentById(ResourceTable.Id_first_category_name_text);
        image.setPixelMap(category.getImage());
        name.setText(category.getName());
        return itemLayout;

    }
}

